package com.kaizeli.website.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.kaizeli.website.mapper.EmployeeMapper;
import com.kaizeli.website.mapper.ProjectBasicInfoMapper;
import com.kaizeli.website.pojo.entity.ProjectBasicInfoDO;
import com.kaizeli.website.pojo.model.EmployeeDO;
import com.kaizeli.website.pojo.dto.ProjectAcceptanceDTO;
import com.kaizeli.website.pojo.vo.ProjectAcceptanceVO;
import com.kaizeli.website.pojo.vo.ProjectStatisticsVO;
import com.kaizeli.website.service.ProjectAcceptanceService;
import com.kaizeli.website.service.ProjectStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 项目统计Service实现类
 */
@Service
public class ProjectStatisticsServiceImpl implements ProjectStatisticsService {
    
    @Autowired
    private ProjectBasicInfoMapper projectBasicInfoMapper;
    
    @Autowired
    private ProjectAcceptanceService projectAcceptanceService;
    
    @Autowired
    private EmployeeMapper employeeMapper;
    
    @Override
    public ProjectStatisticsVO getProjectStatisticsByCode(String projectCode) {
        // 根据项目编号查询项目基本信息
        LambdaQueryWrapper<ProjectBasicInfoDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProjectBasicInfoDO::getProjectCode, projectCode);
        ProjectBasicInfoDO project = projectBasicInfoMapper.selectOne(wrapper);
        
        if (project == null) {
            return null;
        }
        
        // 根据项目编号查询验收单列表
        List<ProjectAcceptanceDTO> acceptances = projectAcceptanceService.getAcceptancesByProjectCode(projectCode);
        
        // 构建统计数据
        ProjectStatisticsVO statistics = new ProjectStatisticsVO();
        statistics.setProjectId(project.getId());
        statistics.setProjectName(project.getProjectName());
        statistics.setProjectCode(project.getProjectCode());
        statistics.setProjectType(project.getProjectType());
        statistics.setProjectTypeName(getProjectTypeName(project.getProjectType()));
        statistics.setStartDate(project.getStartDate());
        statistics.setEndDate(project.getEndDate());
        statistics.setManagerId(project.getManagerId());
        statistics.setManagerName(getManagerName(project.getManagerId()));
        statistics.setAcceptances(acceptances);
        
        // 计算统计数据
        int totalTasks = acceptances.size();
        int completedTasks = (int) acceptances.stream()
                .filter(acceptance -> "已验收".equals(acceptance.getStatus()))
                .count();
        int pendingTasks = totalTasks - completedTasks;
        
        statistics.setTotalTasks(totalTasks);
        statistics.setCompletedTasks(completedTasks);
        statistics.setPendingTasks(pendingTasks);
        
        return statistics;
    }
    
    @Override
    public ProjectStatisticsVO getProjectStatisticsById(Long projectId) {
        // 根据项目ID查询项目基本信息
        ProjectBasicInfoDO project = projectBasicInfoMapper.selectById(projectId);
        
        if (project == null) {
            return null;
        }
        
        // 根据项目ID查询验收单列表
        List<ProjectAcceptanceDTO> acceptances = projectAcceptanceService.getAcceptancesByProjectId(projectId);
        
        // 构建统计数据
        ProjectStatisticsVO statistics = new ProjectStatisticsVO();
        statistics.setProjectId(project.getId());
        statistics.setProjectName(project.getProjectName());
        statistics.setProjectCode(project.getProjectCode());
        statistics.setProjectType(project.getProjectType());
        statistics.setProjectTypeName(getProjectTypeName(project.getProjectType()));
        statistics.setStartDate(project.getStartDate());
        statistics.setEndDate(project.getEndDate());
        statistics.setManagerId(project.getManagerId());
        statistics.setManagerName(getManagerName(project.getManagerId()));
        statistics.setAcceptances(acceptances);
        
        // 计算统计数据
        int totalTasks = acceptances.size();
        int completedTasks = (int) acceptances.stream()
                .filter(acceptance -> "已验收".equals(acceptance.getStatus()))
                .count();
        int pendingTasks = totalTasks - completedTasks;
        
        statistics.setTotalTasks(totalTasks);
        statistics.setCompletedTasks(completedTasks);
        statistics.setPendingTasks(pendingTasks);
        
        return statistics;
    }
    
    /**
     * 获取项目类型名称
     */
    private String getProjectTypeName(String projectType) {
        if (projectType == null) {
            return "未知";
        }
        switch (projectType) {
            case "1":
                return "人力项目";
            case "2":
                return "整包项目";
            case "3":
                return "分包项目";
            default:
                return "未知";
        }
    }
    
    /**
     * 根据员工ID获取员工姓名
     */
    private String getManagerName(Long managerId) {
        if (managerId == null) {
            return "未指定";
        }
        try {
            EmployeeDO employee = employeeMapper.selectById(managerId);
            return employee != null ? employee.getName() : "未知";
        } catch (Exception e) {
            return "未知";
        }
    }
}
